/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tictactoe;
import java.awt.event.* ;
import javax.swing.*;
import javax.swing.border.*;

class Feld extends JButton{
    int x;
    int y;

    Feld(){
        super();
    }
    Feld(int x, int y){
        super();
        this.x=x;
        this.y=y;
    }
}
/**
 *
 * @author renew
 */
public class gui {

    static final JFrame frame = new JFrame();

    static final String picfolder = "/images/";

    public static Icon xIcon = new ImageIcon(picfolder + "x.png");
    public static Icon oIcon = new ImageIcon(picfolder + "o.png");

    static JButton[][] f = new Feld[3][3];
    static JLabel[][] field = new JLabel[3][3];
    static JButton newGame = new JButton();
    static JButton ai = new JButton();

    static final JLabel titel = new JLabel();
    static final JLabel gameOver = new JLabel();
    static final JLabel winner = new JLabel();
    static final JLabel color = new JLabel();

    //repaint der Spielsteine
    static void refresh (Element[][] feld){
        for(int i=0; i<3; i++){
            for (int j=0; j<3; j++){
                if (feld [i][j]!=null){
                    if (feld[i][j].cross) field[j][i].setText("x");
                    if (!feld[i][j].cross) field[j][i].setText("o");
                    f[j][i].setVisible(false);
                }
                else
                {
                    field[j][i].setText("");
                    f[j][i].setVisible(true);
                }

            }
        }
    }

    final static JButton startWith = new JButton();

    //erstmaliger Aufbau der gui
    static void init(){

        //9 Buttons
        for (int i=0; i<3; i++){
            for (int j=0; j<3; j++){
                final Feld feld = new Feld(i,j);
                frame.add(feld);
                f[i][j]=feld;
                feld.setSize(50, 50);
                feld.setLocation(50*i+100, 50*j+100);
                feld.setBorder(LineBorder.createBlackLineBorder());
                feld.addActionListener(new ActionListener(){
                    @Override public void actionPerformed( ActionEvent e){
                        Main.action(feld.y, feld.x);
                    }
                });
                final JLabel xo = new JLabel();
                final JLabel border = new JLabel();
                frame.add(xo);
                frame.add(border);
                field[i][j]=xo;
                border.setSize(50, 50);
                border.setLocation(50*i+100, 50*j+100);
                border.setBorder(LineBorder.createBlackLineBorder());
                border.setVisible(true);
                xo.setSize(50, 50);
                xo.setLocation(50*i+120, 50*j+100);
                xo.setVisible(true);
            }
        }

        startWith.setVisible(true);
        frame.add(startWith);
        startWith.setSize(60, 35);
        startWith.setLocation(50, 50);
        startWith.setText("x");
        startWith.addActionListener(new ActionListener(){
            @Override public void actionPerformed( ActionEvent e){
                Main.turn= !Main.turn;
                startWith.setText(Main.turn? "x" : "o");
            }
        });

        frame.add(newGame);
        newGame.setSize(150, 50);
        newGame.setLocation(250, 300);
        newGame.setText("new Game");
        newGame.addActionListener(new ActionListener(){
            @Override public void actionPerformed( ActionEvent e){
                Main.newGame();
            }
        });


        //Schriften setzen
        frame.add(titel);
        titel.setText("Tic Tac Toe");
        titel.setLocation(150, 0);
        titel.setSize(200,40);
        titel.setVisible(true);

        frame.add(gameOver);
        gameOver.setText("Game Over:");
        gameOver.setLocation (100,300);
        gameOver.setSize(200, 40);
        gameOver.setVisible(false);

        frame.add(winner);
        winner.setLocation (100,320);
        winner.setSize(250, 40);
        winner.setVisible(true);

        frame.add(color);
        color.setText("Wählen:");
        color.setLocation(50, 20);
        color.setSize(200,40);
        color.setVisible(true);

        frame.add(ai);
        ai.setText("AI is on");
        ai.setLocation(300, 20);
        ai.setSize(120,40);
        ai.setVisible(true);
        ai.addActionListener(new ActionListener(){
            @Override public void actionPerformed( ActionEvent e){
                Main.ai_use = !Main.ai_use;
                ai.setText(Main.ai_use? "AI is on" : "AI is off");
            }
        });

        //blinder background button, damit die 9 funzen
        JButton bg = new JButton();
        frame.add(bg);
        bg.setVisible(false);

        //frame-settings
        frame.setTitle("Tic Tac Toe");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(50,50,450,400);
        frame.setVisible(true);
    }
}
